//
//  SMBDatePickerView.m
//  SMBPickerView
//
//  Created by Zongzhu on 16/4/11.
//  Copyright © 2016年 Zongzhu. All rights reserved.
//

#import "SMBDatePickerView.h"
#import "NSDate+Additions.h"

@interface SMBDatePickerView ()
@property (nonatomic, strong) NSDate *date;
@property (nonatomic, assign) SMBDatePickerType model;
@property (nonatomic, strong) UIDatePicker *pickerView;
@end
@implementation SMBDatePickerView
@dynamic pickerView;

+ (void)showDatePickerNormalPickerMode:(SMBDatePickerType)model date:(NSDate *)date completion:(void (^)(NSString *))completion{
    [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
    SMBDatePickerView *pickerView = [[self alloc] initWithFrame:CGRectZero];
    pickerView.date = date;
    pickerView.model = model;
    [pickerView setPickerHidden:NO callBack:nil];
    pickerView.onDismissCompletion = completion;
    pickerView.pickerView.minimumDate = [NSDate dateWithTimeIntervalSince1970:0];
    [self.rootView addSubview:pickerView];
}

+(void)showDatePickerViewWithDatePickerMode:(SMBDatePickerType)model date:(NSDate *)date completion:(void (^)(NSString *))completion{
    [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
    SMBDatePickerView *pickerView = [[self alloc] initWithFrame:CGRectZero];
    pickerView.date = date;
    pickerView.model = model;
    [pickerView setPickerHidden:NO callBack:nil];
    pickerView.onDismissCompletion = completion;
    pickerView.pickerView.maximumDate = [NSDate date];
    [self.rootView addSubview:pickerView];
}
+(void)showDatePickerViewWithAfterDatePickerMode:(SMBDatePickerType)model date:(NSDate *)date completion:(void (^)(NSString *))completion{
    [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
    SMBDatePickerView *pickerView = [[self alloc] initWithFrame:CGRectZero];
    pickerView.date = date;
    pickerView.model = model;
    [pickerView setPickerHidden:NO callBack:nil];
    pickerView.onDismissCompletion = completion;
    pickerView.pickerView.minimumDate = [NSDate date];
    pickerView.pickerView.maximumDate = [NSDate dateWithString:@"2100-12-31" format:@"yyyy-MM-dd"];
    [self.rootView addSubview:pickerView];
}
+(void)showDatePickerViewWithLimitDatePickerMode:(SMBDatePickerType)model date:(NSDate *)date completion:(void (^)(NSString *))completion{
    SMBDatePickerView *pickerView = [[self alloc] initWithFrame:CGRectZero];
    pickerView.date = date;
    pickerView.model = model;
    [pickerView setPickerHidden:NO callBack:nil];
    pickerView.onDismissCompletion = completion;
    pickerView.pickerView.minimumDate = [NSDate dateWithTimeIntervalSince1970:0];
    pickerView.pickerView.maximumDate = [NSDate date];
    [self.rootView addSubview:pickerView];
}
+(void)showDatePickerViewWithLimitDatePickerMode:(SMBDatePickerType)model cancel:(NSString *)cancel complete:(NSString *)complete date:(NSDate *)date cancelHandle:(void (^)(NSString *))cancelHandel completionHandle:(void (^)(NSString *))completionHandle{
    SMBDatePickerView *pickerView = [[self alloc] initWithFrame:CGRectZero];
    [pickerView.cancelButton setTitle:cancel forState:UIControlStateNormal];
    [pickerView.completeButton setTitle:complete forState:UIControlStateNormal];
    pickerView.date = date;
    pickerView.model = model;
    [pickerView setPickerHidden:NO callBack:nil];
    pickerView.onDismissCompletion = completionHandle;
    pickerView.onDismissCancel = cancelHandel;
    pickerView.pickerView.minimumDate = [NSDate dateWithTimeIntervalSince1970:0];
    pickerView.pickerView.maximumDate = [NSDate date];
    [self.rootView addSubview:pickerView];
}
#pragma mark -
- (id)pickerViewWithRect:(CGRect)rect{
    self.pickerView = [[UIDatePicker alloc] initWithFrame:rect];
    self.pickerView.backgroundColor = [UIColor whiteColor];
    return self.pickerView;
}
- (void)initializePickerView{
    switch (self.model) {
        case SMBDatePickerTypeTime: {
            self.pickerView.datePickerMode = UIDatePickerModeTime;
        }
            break;
        case SMBDatePickerTypeDate: {
            self.pickerView.datePickerMode = UIDatePickerModeDate;
            self.pickerView.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
        }
            break;
        case SMBDatePickerTypeDateAndTime: {
            self.pickerView.datePickerMode = UIDatePickerModeDateAndTime;
        }
            break;
        case SMBDatePickerTypeDateZh:
        {
            self.pickerView.datePickerMode = UIDatePickerModeDate;
            self.pickerView.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
        }
            break;
        default:
            break;
    }
    self.pickerView.date = self.date;
}
- (id)selectedObject{
    NSString *format = nil;
    
    switch (self.model) {
        case SMBDatePickerTypeTime: {
            format = @"HH:mm:ss";
        }
            break;
        case SMBDatePickerTypeDate: {
            format = @"yyyy-MM-dd";
        }
            break;
        case SMBDatePickerTypeDateAndTime: {
            format = @"yyyy-MM-dd HH:mm:ss";
        }
            break;
        case SMBDatePickerTypeDateZh:
        {
            format = @"yyyy年MM月";
        }
            break;
        default:
            break;
    }
    return [self.pickerView.date stringWithFormat:format];
}
@end
